<?php

/**
 * 数据获取基本类
 * @author Ice iceinto@mallog.com.cn
 */
class BaseModel extends Model
{

    /**
     * 根据标示默认获取数据信息
     * @param int $id 标示
     * @param string $fields 字段
     * @return array
     * @author Ice  <iceinto@mallog.com>
     */
    protected function getById($id, $fields = null)
    {
        return $this->field($fields)->find($id);
    }

    /**
     * 根据标示默认获取数据信息
     * @param array $map 查询条件
     * @param string $fields 字段
     * @param string $order 排序
     * @return array
     * @author Ice <iceinto@mallog.com>
     */
    protected function getByWhere($map = null, $fields = null, $order = null)
    {
        return $this->where($map)->field($fields)->order($order)->find();
    }

    /**
     * 获取数据列表
     * @param array $map 查询条件
     * @param string $limit 分页
     * @param string $order 排序
     * @param string $fields 字段
     * @return array
     * @author Ice <iceinto@mallog.com>
     */
    protected function getListByWhere($map = null, $fields = null, $limit = null, $order = null)
    {
        return $this->where($map)->limit($limit)->order($order)->field($fields)->select();
    }

    /**
     * 获取条件数量
     * @param array $where
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getWhereCount($where)
    {
        return $this->where($where)->count();
    }

    /**
     * 获取地理位置
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function getCity()
    {
        //默认定位获取
        //$city = Cookie::get('home');
        return 320100;
    }

    public function getThisDayTime()
    {
        $date = new Date();
        $dayTime = $date->format('%Y-%m-%d');
        return strtotime($dayTime);
    }

    /**
     * 根据类型获取图片完整显示
     * @param String $type 上传类型
     * @param String $isDate 是否有时间设置
     * @return String 图片地址
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getManagedUrl($id, $type = '', $isDate = false)
    {
        $defaultImages = UPLOAD_URL . 'default.jpg';
        $FileManagedModel = M('FileManaged' . ucwords($type));
        $map = array();
        if ($id) {
            $map['id'] = $id;
            $fImages = $FileManagedModel->where($map)->find();
            if ($fImages) {
                if ($type && !$isDate) {
                    return UPLOAD_URL . $type . '/' . $fImages['uri'];
                } else if ($type && $isDate) {
                    //解析时间
                    $year = strftime('%Y', $fImages['time']);
                    $date = strftime('%m%d', $fImages['time']);
                    //返回时间地址
                    return UPLOAD_URL . $type . '/' . $year .
                        '/' . $date . '/' . $fImages['uri'];
                } else {
                    return UPLOAD_URL . $fImages['uri'];
                }
            }
            return $defaultImages;
        }
        return $defaultImages;
    }

    /**
     * 根据条件返回广告连接地址
     * @param $mid
     * @param int $fid
     * @param int $bid
     * @return array
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getAdUrl($mid, $fid = 0, $bid = 0)
    {
        $map = array();
        $url = null;
        $mall = null;
        $floor = null;
        $brand = null;
        //TODO 添加缓存 优化
        //获取商场信息
        if ($mid) {
            unset($map);
            $map['id'] = $mid;
            $mall = M('Market')->field('initial')->where($map)->find();
        }
        //获取楼层信息
        if ($fid) {
            unset($map);
            $map['id'] = $fid;
            $floor = M('Floor')->field('sain')->where($map)->find();
        }
        //获取品牌信息
        if ($bid) {
            unset($map);
            $map['id'] = $bid;
            $brand = M('Brand')->field('ename')->where($map)->find();
        }
        if ($mall && $floor && $brand) {
            $url = U('/' . $mall['initial'] . '/' . $floor['sain'] . '/' . $brand['ename']);
        } else if ($mall && $floor) {
            $url = U('/' . $mall['initial'] . '/' . $floor['sain']);
        } else if ($mall) {
            $url = U('/' . $mall['initial']);
        } else if ($brand) {
            $url = U('search/' . $brand['ename']);
        }

        return strtolower($url);
    }


    /**
     * 根据条件返回基本商场楼层品牌信息
     * @param $mid
     * @param int $fid
     * @param int $bid
     * @return array
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getAdMFB($mid, $fid = 0, $bid = 0)
    {
        $map = array();
        $mall = null;
        $floor = null;
        $brand = null;
        $data = array();
        //获取商场信息
        if ($mid) {
            unset($map);
            $map['id'] = $mid;
            $data['mall'] = M('Market')->field('initial,name,image')->where($map)->find();
        }
        //获取楼层信息
        if ($fid) {
            unset($map);
            $map['id'] = $fid;
            $data['floor'] = M('Floor')->field('mid,sain,name')->where($map)->find();
        }
        //获取品牌信息
        if ($bid) {
            unset($map);
            $map['id'] = $bid;
            $data['brand'] = M('Brand')->field('id,name,ename,initials,image,small_image')->where($map)->find();
        }

        return $data;
    }

    /**
     * 获取品牌图片地址
     * @param int $id
     * @return String 图片地址
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getImageUrl($id)
    {
        return $this->getManagedUrl($id, $this->_type);
    }

    /**
     * 默认结果返回组合
     * @param Array $data
     * @param  $status
     * @return Array 返回结果信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function mReturn($data = null, $status = false)
    {
        $result = array();
        $result['status'] = $status;
        is_null($data) ? null : $result['data'] = $data;
        return $result;
    }

    protected function _after_insert($data, $options)
    {
    }

    protected function _after_update($data, $options)
    {
    }

    protected function _after_delete($data, $options)
    {
    }
}

?>